import 'package:flutter/material.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:flutter_syt/api/api.dart';
import 'package:flutter_syt/datas/number_extension.dart';
import 'package:flutter_syt/datas/station_item.dart';
import 'package:flutter_syt/datas/user_info.dart';
import 'package:flutter_syt/domain/message/message_sub_list.dart';
import 'package:flutter_syt/domain/widgets/common_dialog.dart';
import 'package:flutter_syt/domain/widgets/custom_dialog.dart';
import 'package:flutter_syt/generated/l10n.dart';
import 'package:flutter_syt/global_easy_loading.dart';
import 'package:flutter_syt/theme/theme_color.dart';
import 'package:flutter_syt/utils/click_utils.dart';
import 'package:flutter_syt/utils/global_date_time_format.dart';
import 'package:flutter_syt/utils/utils_dlog.dart';
import 'package:flutter_syt/utils/utils_other.dart';
import 'package:flutter_syt/widgets/normal_table.dart';
import 'package:yyy_route_annotation/yyy_route_annotation.dart';

class MessageItemEntity {
  late String imageUrl;
  late String title;
  late int time;
  late String content;
  late String pushlishuserid;
  late bool isRead;

  MessageItemEntity(this.imageUrl, this.title, this.pushlishuserid, this.time,
      this.content, this.isRead);
}

@RoutePage(name: "message")
class MessagePage extends StatefulWidget {
  @override
  _MessagePageState createState() => _MessagePageState();
}

class _MessagePageState extends State<MessagePage> {
  NormalTableViewController _tableController = NormalTableViewController();
  @override
  void initState() {
    super.initState();
  }

  _showCleanUnreadMsg() {
    DLog('asdfsd');
    showCommonDialog(
      context,
      DialogEntity(
        S.of(context).msg_cleanUnread,
        Text(''),
        () => {Navigator.of(context).pop()},
        () async {
          await loading(() async {
            // _tableController.refresh();
            // pushlish_user_id
            try {
              await API.shared.station.putCleanUnread();
            } catch (e) {
              UtilsOther.showCustomShowError(e);
            }
            // bool readSuccess = await API.shared.station.putCleanUnread();
            // if (readSuccess) {
            _tableController.refresh();
            // }
          }, text: '');

          Navigator.of(context).pop();
        },
        cancelText: S.of(context).cancel,
        okText: S.of(context).confirm,
        contentPadding: EdgeInsets.all(0),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          S.of(context).notifications,
        ),
        actions: [
          GestureDetector(
            onTap: ClickUtils.debounce(() {
              _showCleanUnreadMsg();
            }),
            child: Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                Image.asset(
                  'assets/images/icon_msg_unread.png',
                  width: 16,
                ),
                Container(
                  margin: EdgeInsets.only(left: 4),
                ),
                Text(
                  S.current.msg_allUnread,
                  style: TextStyle(
                      color: Colors.black.withOpacity(0.6), fontSize: 12),
                ),
                SizedBox(
                  width: 15,
                )
              ],
            ),
          )
        ],
      ),
      body: NormalTableView<StationItem>(
        controller: _tableController,
        itemBuilder: (ctx, e) {
          var item = MessageItemEntity(
            e.account_logo,
            e.account_name,
            e.pushlish_user_id,
            parseInt(e.updated_at),
            e.content,
            parseInt(e.is_read) == 2,
          );
          return GestureDetector(
              onTap: ClickUtils.debounce(() {
                setState(() {
                  e.is_read = 1;
                });
                Navigator.of(context).push(
                    MaterialPageRoute(builder: (ctx) => MessageSubList(e)));
              }),
              child:  Slidable(
                enabled: true,
                // startActionPane: const SlidableDrawerActionPane(),
                // actionExtentRatio: 0.24,
                // actionPane: const SlidableDrawerActionPane(),
                // secondaryActionDelegate: SlideActionBuilderDelegate(
                //   actionCount: 1,
                //   builder: (context, index, animation, step) {
                //     return SlideAction(
                //       child: Container(
                //         color: Color(0xFFFA4636),
                //         // width: ScreenUtil().setWidth(60),
                //         alignment: Alignment.center,
                //         child: Text(
                //           S.current.del_title,
                //           style: TextStyle(
                //             color: Color(0xFFFFFFFF),
                //           ),
                //         ),
                //       ),
                //       onTap: ClickUtils.debounce(() async {
                //         await loading(() async {
                //           // pushlish_user_id
                //           bool isSuccess = await API.shared.station
                //               .postDeleteMsg(item.pushlishuserid);
                //           if (isSuccess) {
                //             _tableController.refresh();
                //           }
                //         }, text: '');
                //       }),
                //     );
                //   },
                // ),
                child: _messageList(item),
              ),

              // child: _messageList(item),
              );
        },
        getData: (page) async {
          if (UserInfo.me.isLogin) {
            var list = await API.shared.station.list(page);
            return list;
          } else {
            return [];
          }
        },
      ),
    );
  }

  Widget _messageList(MessageItemEntity message) {
    return Container(
      padding: EdgeInsets.fromLTRB(18, 20, 12, 20),
      decoration: BoxDecoration(
        border: Border(
            bottom: BorderSide(width: 0.5, color: ThemeColor.dividerColor)),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Stack(
            children: [
              Container(
                margin: EdgeInsets.only(right: 14),
                width: 44,
                height: 44,
                child: CircleAvatar(
                  radius: 44,
                  backgroundImage: NetworkImage(message.imageUrl),
                ),
              ),
              message.isRead
                  ? Positioned(
                      right: 15,
                      child: Container(
                        decoration: BoxDecoration(
                          color: Color(0xffFA4636),
                          shape: BoxShape.circle,
                        ),
                        width: 10,
                        height: 10,
                      ),
                    )
                  : Text(''),
            ],
          ),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      message.title,
                      style: TextStyle(
                        fontSize: 16,
                        color: ThemeColor.colorBlack80,
                        fontWeight: FontWeight.w500,
                      ),
                    ),
                    Text(
                      formatDate(message.time),
                      style: TextStyle(
                        fontSize: 12,
                        color: Color(0xff999999),
                        fontWeight: FontWeight.w400,
                      ),
                    ),
                  ],
                ),
                Padding(
                  padding: EdgeInsets.only(top: 5),
                  child: Text(
                    message.content,
                    softWrap: true,
                    textAlign: TextAlign.left,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(
                      fontSize: 12,
                      color: ThemeColor.colorBlack60,
                      fontWeight: FontWeight.w300,
                    ),
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
